﻿using System;
using NsbExtensions.Messages.Management.Monitoring;

namespace NsbExtensions.Endpoint.Management.Monitoring
{   //not threadsafe
    public abstract class EndpointStatusProbeBase : IEndpointStatusProbe

    {
        private bool _canAggregate = true;

        public string Name { get; set; }
        public string UnitOfMeasurement { get; set; }
        public IMessagingEndpoint Endpoint { get; private set; }

        public bool CanAggregate
        {
            get { return _canAggregate; }
            set { _canAggregate = value; }
        }

        public virtual EndpointMeasurement TakeMeasurement(IMessagingEndpoint endpoint)
        {
            Endpoint = endpoint;

            object value = null;

            MeasurementDataType dataType = MeasurementDataType.Numeric;
            
            TakeMeasurement(ref value, ref dataType);

            return CreateEndpointMeasurement(Name,value,UnitOfMeasurement,CanAggregate,dataType);
        }

        object ICloneable.Clone()
        {
            return MemberwiseClone();
        }

        public EndpointStatusProbeBase Clone()
        {
            return (EndpointStatusProbeBase)((ICloneable)this).Clone();
        }

        protected virtual EndpointMeasurement CreateEndpointMeasurement(string name,object value,string unitOfMeasurement,bool canAggregate,MeasurementDataType dataType)
        {
            return new EndpointMeasurement
            {
                Name = name,
                Value = value != null ? value.ToString() : String.Empty,
                UnitOfMeasurement = unitOfMeasurement,
                CanAggregate = canAggregate,
                DataType = dataType
            };
        }

        protected abstract void TakeMeasurement(ref object value,ref MeasurementDataType dataType);
      
    }
}
